package uit.edu.vn.Activity;


import uit.edu.vn.Utility.CheckInternet;
import uit.edu.vn.Utility.MyActivity;
import uit.edu.vn.Utility.WebService;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class RegistryActivity extends MyActivity{
	private Button btn_registry;
	private String username,password,cf_password,email,results;
	private ProgressDialog loadingRegistryDialog;
	private EditText edt_username,edt_password,edt_confirm_pass,edt_email;
	private Context context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registry);
		context=getBaseContext();
		btn_registry = (Button)findViewById(R.id.btn_DangKy);
		edt_username = (EditText)findViewById(R.id.edt_account);
		edt_password = (EditText)findViewById(R.id.edt_password);
		edt_confirm_pass = (EditText)findViewById(R.id.edt_confirmPassword);
		edt_email = (EditText)findViewById(R.id.edt_Email);
		loadingRegistryDialog= new ProgressDialog(this);
		loadingRegistryDialog.setMessage("Đang đăng ký tài khoản !");
		loadingRegistryDialog.setCancelable(false);
		loadingRegistryDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		btn_registry.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				   CheckInternet chkI= new CheckInternet(context);
				   if(chkI.checkStatus().equals(true))
					{
					   try{
							username = edt_username.getText().toString();			
							password = edt_password.getText().toString();				
							cf_password = edt_confirm_pass.getText().toString();			
							email = edt_email.getText().toString();
							if(username.equals("")||password.equals("")||cf_password.equals("")||email.equals("")){
								showToast("Vui lòng điền đầy đủ thông tin!");
							}else if(!password.equals(cf_password)){
								showToast("Mật khẩu không khớp");
							}else if(!checkEmail(email))
								showToast("Email không hợp lệ");
							else if(!checkAccount(username))
								showToast("Tài khoản không hợp lệ");
							else{
								new BackGroundTask().execute();
							}
//							// kiem tra ket qua tra ve
//							if(result.equals("0")){
//								showToast("Tài khoản đã tồn tại!");
//							}else if(result.equals("-2")){
//								showToast("Email đã tồn tại!");
//							}else if(result.equals("-1")){
//								showToast("Đăng ký tài khoản thất bại!");
//							}else if(result.equals("1")){
//								showToast("Tạo tài khoản thành công!");
//						        Intent intent = new Intent(RegistryActivity.this, LoginActivity.class);
//						        startActivity(intent);
//							}
						}catch(Exception ex){
							System.out.println("dang ky tai khoan khong thanh cong");
							loadingRegistryDialog.dismiss();
						}			        	
					}
			        else
			        {
			        	Log.d("ketnoi", "ko co ket noi internet");
			        	showToast("Khong co ket noi internet");
			        
			        }
				
			}
		});
	}
	private class BackGroundTask extends AsyncTask<Void, Void, Void>
	{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			loadingRegistryDialog.show();
		}
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				results = WebService.createAccount(username, password, email);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			loadingRegistryDialog.dismiss();
			// kiem tra ket qua tra ve
			if(results.equals("0")){
				showToast("Tài khoản đã tồn tại!");
			}else if(results.equals("-2")){
				showToast("Email đã tồn tại!");
			}else if(results.equals("-1")){
				showToast("Đăng ký tài khoản thất bại!");
			}else if(results.equals("1")){
				showToast("Tạo tài khoản thành công!");
		     //   Intent intent = new Intent(RegistryActivity.this, LoginActivity.class);
		       // startActivity(intent);
		      //  finish();
			}
			super.onPostExecute(result);
		}
	}
	
	public static boolean checkEmail(String email) {
		return email.matches("[a-zA-Z0-9\\-_]+(\\.[a-zA-Z0-9\\-_]+)*@[a-zA-Z0-9\\-_]+(\\.[a-zA-Z0-9\\-_]+)+");
	}
	public static boolean checkAccount(String account) {
		return account.matches("([a-zA-Z0-9_\\-\\.]+)");
	}
	
  
}
